#/*************************************************************************
#    > File Name: examples/example_File.py
#    > Author: Yan Wang
#    > Mail: wangyan@imnu.edu.cn
#    > Created Time: 2022年06月22日 星期三 15时04分55秒
# ************************************************************************/
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.path.append('../Lib/')
from Load_Lib import * 

print("The examples in this file are:")   
ex_name={"1":"Get_File_Name"
,"2":"File_Name_Generalizing"
,"3":"Copy_File"
,"4":"Move_File"
,"5":"File_Replace"
,"6":"File_Insert"
,"7":"Generate_General_NameList"
,"8":"Create_MultiFile"
,"9":"Copy_Multi_File"
,"10":"Rename_Series_Files"
,"11":"Multi_Rename_Series_Files"
,"12":"Move_Series_Files"
,"13":"File_Exist"
,"14":"File_Exist_in_a_Folder"
,"15":"File_Exist_in_Folders"
,"all":"all examples"}    #字典，所有对应函数为Lib/File_Operation.py
for key in ex_name:
	print(key, ex_name[key])       #if条件语句，如果key在ex_name里面，则输出eg：1 Get_File_Name
num=input("请输入想要运行的例子：")


if num == "1" or num == ex_name["1"] or num == "all":
# Get_File_Name
# 一个参数--文件名称
# 返回参数的基文件名(最底层的文件)
# 最后输出为this_file.dat
	path_name="../Lib/this_file.dat"
	Exhibit_Function(Get_File_Name, [path_name])
	Exhibit_Function(Get_File_Name_without_Ext, [path_name])


if num == "2" or num == ex_name["2"] or num == "all":
# File_Name_Generalizing
# 一个参数--文件名称
# 将文件名中添加 _{} ， 便于后续扩展为其他类似的文件名
# 最后输出为his_file_{}.dat文件
	path_name="../Lib/this_file.dat"
	Exhibit_Function(File_Name_Generalizing, [path_name])


if num == "3" or num == ex_name["3"] or num == "all":
# Copy_File
# 两个参数(A,B),A为被复制的文件,B目标文件
# copy多个source_file目标文件内的内容复制到target_file中且命名target_file_1.(2...).dat  
# 最后输出为复制完成的文件./examples/test_output/test_file.dat
	source_file="./examples/test_input/test_file.dat"
	target_file="./examples/test_output/test_file.dat"
	Exhibit_Function(Copy_File,[source_file,target_file])


if num == "4" or num == ex_name["4"] or num == "all":
# Move_File
# 两个参数(A,B),A为源文件,B目标文件
# 将source_file移动到target_file并返回target_file，否则出现语法提示或报错
# 最后输出为移动完成的文件./examples/test_output/test_file_moved.dat
	source_file="./examples/test_output/test_file.dat"
	target_file="./examples/test_output/test_file_moved.dat"
	Exhibit_Function(Move_File,[source_file,target_file])


if num == "5" or num == ex_name["5"] or num == "all":
# File_Replace
# 三个参数(A,B,C),A为被替换的文件名,B为被替换行数列表,C为被替换内容
# 将file_name中的第4行替换为replace_change_1，同样的将第6行替换为replace_change_2
# 最后输出结果为["replace_change_1", "replace_change_2"]
	file_name="./examples/test_output/test_file_moved.dat"
	linenos=[4, 6]
	strings=["replace_change_1", "replace_change_2"]
	Exhibit_Function(File_Replace,[file_name,linenos,strings])


if num == "6" or num == ex_name["6"] or num == "all":
# File_Insert
# 三个参数(A,B,C),A为被替换的文件名,B为被替换行数列表,C为被替换内容
# 将file_name中的第4行替换为replace_change_1并且插入第4行原有的内容，同样的将第6行替换为replace_change_2且插入第6行原有的内容
# 最后输出为["insert_change_1", "insert_change_2"]
	file_name="./examples/test_output/test_file_moved.dat"
	linenos=[10, 13]
	strings=["insert_change_1", "insert_change_2"]
	Exhibit_Function(File_Insert,[file_name,linenos,strings])

# Move_Series_Files
# 用于MG下生成的 run_01 文件迁移， 不方便举例


if num == "7" or num == ex_name["7"] or num == "all":
# Generate_General_NameList 
# 三个参数(A,B,C),A为生成文件的名字,B为生成文件所在目录,C为生成文件的个数
# 生成同一类文件的文件名列表
# 最后输出为：./examples/test_output/test_（1---5）.dat
	file_general_name="test_{}.dat"
	target_folder="./examples/test_output/"
	# 第一个参数有两种输入格式，数字的列表 或者 一个数字
	file_num_list=[1,3,5]
	Exhibit_Function(Generate_General_NameList,[file_general_name,target_folder,file_num_list])
	filelength=3
	Exhibit_Function(Generate_General_NameList,[file_general_name,target_folder,filelength])


if num == "8" or num == ex_name["8"] or num == "all":
# Create_MultiFile
# 四个参数(A,B,C,D),A为生成文件个数,B为最后生成的文件名称,C为生成文件的目录,D被复制的文件内容(复制到多个B中)
# 根据的文件名列表，一次copy生成多个文件target_folder/general_name(1-filelength)
# 最后输出为./examples/test_output/test_（1---10）.dat
	file_general_name="test_{}.dat"
	target_folder="./examples/test_output/"
	original_file="./examples/test_input/test_file.dat"
	file_num_list=[1,3,5]
	Exhibit_Function(Create_MultiFile,[file_num_list,file_general_name,target_folder,original_file])
	filelength=10
	Exhibit_Function(Create_MultiFile,[filelength,file_general_name,target_folder,original_file])



if num == "9" or num == ex_name["9"] or num == "all":
# Copy_Multi_File
# 三个参数(A,B,C),A需要生成文件的个数(命名),B为被复制的基文件,C为寻找指定路径并获取目录
# 给定文件original_file,寻找到其基文件，复制到指定文件夹file_general_name下，并且在生成多个子文件
# 最后输出为./examples/test_output/test_file/test_(1--10).dat
	file_general_name="./examples/test_output/test_{}.dat"
	original_file="./examples/test_input/test_file.dat"
	file_num_list=[1,3,5]
	Exhibit_Function(Copy_Multi_File,[file_num_list,original_file,file_general_name])
	filelength=10
	Exhibit_Function(Copy_Multi_File,[filelength,original_file,file_general_name])


if num == "10" or num == ex_name["10"] or num == "all":
# Rename_Series_Files
# 四个参数(A,B,C,D),A表示替换的字符或函数,B为要被操作的那个文件,C为对B进行切片操作的分隔符,D为重新命名的个数
# 将name_series替换到file_name基文件中,以split分割
# 无返回值但是./examples/test_input/ARCH_output/sigh_w4b_3.dat的文件被命名为./examples/test_input/ARCH_output/sigh_w4b_10.dat
	file_name="./examples/test_input/ARCH_output/sigh_w4b_3.dat"
	name_series="10"
	split="_"
	num=2
	Exhibit_Function(Rename_Series_Files,[name_series,file_name,split,num])


if num == "11" or num == ex_name["11"] or num == "all":
# Multi_Rename_Series_Files
# 四个参数(A,B,C,count=0),A为内置函数re.sub()表示正则中的模式字符串,B为被替换的路径目录,C表示替换的字符串或函数,count=0被用于内置函数re.sub()中,代表所有的匹配都会被替换；
# 统一命名:将test_folder文件夹下的sigh_w4b_\d+统一改为sigh_1
	source_folder="./examples/test_input/ARCH_output/"
	test_folder="./examples/test_output/ARCH_output/"
	pattern="sigh_w4b_\d+"
	repl="sigh_1"
	Copy_File(source_folder,test_folder)
	Exhibit_Function(Multi_Rename_Series_Files,[pattern,test_folder,repl])


if num == "12" or num == ex_name["12"] or num == "all":
# Move_Series_Files 
# 四个参数(A,B,C,D),A为最后想要的基文件命名,B为源基文件,C为源目录,D目标目录
# 首先要将source_folder复制为test_folder并且对其进行备份,命名为test_folder_1即可,再将original_folder下的sub_folder_pattern目录下的origin_file进行替换为general_file以及复制到target_folder
# 最后输出为：Backup old ./examples/test_output/ARCH_output/ as ./examples/test_output/ARCH_output_14
# ['./examples/test_output/ARCH_collecti/t_02.dat',
#  './examples/test_output/ARCH_collecti/t_03.dat',
#  './examples/test_output/ARCH_collecti/t_01.dat']
	source_folder="./examples/test_input/ARCH_output/"
	test_folder="./examples/test_output/ARCH_output/"
	Copy_File(source_folder,test_folder)
	original_folder="./examples/test_output/ARCH_output/Event_1"
	sub_folder_pattern="run"
	general_file="t_{}.dat"
	origin_file="t_1.dat"
	target_folder="./examples/test_output/ARCH_collect/"
	Move_Series_Files(general_file,origin_file,original_folder,target_folder)


if num == "13" or num == ex_name["13"] or num == "all":
	file_name="/home/yancy/Software/MG5/Events_mh_mch_10/THDM_CH_4b0j_1/Events/run_01/tag_1_delphes_events.root"
	file_pattern="tag_1_delphes_events"
	Exhibit_Function(File_Exist,[file_name,file_pattern])


if num == "14" or num == ex_name["14"] or num == "all":
	folder_name="/home/yancy/Software/MG5/Events_mh_mch_10/THDM_CH_4b0j_1/Events/run_01"
	file_pattern="tag_1_delphes_events"
	Exhibit_Function(File_Exist_in_a_Folder, [folder_name,file_pattern])
	File_Exist_in_a_Folder(folder_name,file_pattern)

if num == "15" or num == ex_name["15"] or num == "all":
	folder_name="/home/yancy/Software/MG5/Events_mh_mch_10/THDM_CH_4b0j_{}/Events"
	file_pattern="tag_1_delphes_events"
	Exhibit_Function(File_Exist_in_Folders, [folder_name,file_pattern])
	for num in range(90):
		files_in_folders, folders_not_include_file=File_Exist_in_Folders(folder_name.format(num),file_pattern)
		print(files_in_folders)
		print(folders_not_include_file)


